﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class HoaDonNhap_Dao
    {
        private readonly BaseOperationImplement<HoaDonNhap_Dto>  _baseOperationImp  = new BaseOperationImplement<HoaDonNhap_Dto>();
        public DataSet GetAllObject()
        {
            string query = @"SELECT hdn.MaHDN, hdn.MaNV, hdn.MaNCC, cthd.MaSP, hdn.NgayNhap, 
		                        cthd.SoLuong, sp.GiaNhap,cthd.KhuyenMai, cthd.ThanhTien
                                FROM dbo.HoaDonNhap hdn 
                                join dbo.ChiTietHDN cthd 
                                ON hdn.MaHDN = cthd.MaHDN
                                join dbo.SanPham sp 
                                on cthd.MaSP = sp.MaSP
                                ORDER BY hdn.MaHDN DESC";

            return _baseOperationImp.GetDataSet(query);
        }

        public DataSet GetHoaDons(string mancc)
        {
            return _baseOperationImp.GetDataSet(@"select * 
            from dbo.HoaDonNhap hdn join dbo.NhaCungCap ncc
            on hdn.MaNCC = ncc.MaNCC
            where ncc.MaNCC  = '" + mancc + "'");
        }

        public int AddObject(string mahdn, string ngaynhap, string manv, string mancc, string masp, int soluong, int khuyenmai, decimal gianhap)
        {
            return _baseOperationImp.AddObject("insHDN", CommandType.StoredProcedure, "@mahdn", mahdn, "@ngaynhap", ngaynhap,
                "@manv", manv, "@mancc", mancc, "@masp", masp, "@soluong", soluong, "@km", khuyenmai, "@gianhap", gianhap);
        }

        public int EditObject(string mahdn, string ngaynhap, string manv, string mancc, string masp, int soluong, int khuyenmai, decimal gianhap)
        {
            return _baseOperationImp.EditObject("updateHdn", CommandType.StoredProcedure, "@mahdn", mahdn, "@ngaynhap", ngaynhap,
                "@manv", manv, "@mancc", mancc, "@masp", masp, "@soluong", soluong, "@km", khuyenmai, "@gianhap", gianhap);
        }

        public int DeleteObject(string mdHdn)
        {
            return _baseOperationImp.DeleteObject("delHDN", CommandType.StoredProcedure, "@id", mdHdn);
        }

        public int AutoIncrementIdObject()
        {
            return _baseOperationImp.AutoIncrementIdObject("HoaDonNhap", "MaHDN"); 
        }

        public DataTable FindObject(string condition)
        {
            var helper = new Helper();
            return helper.GetDataTable(@"SELECT * 
                                        FROM HoaDonNhap hdn join dbo.ChiTietHDN cthdn
                                        on hdn.MaHDN = cthdn.MaHDN
                                        WHERE cthdn." + condition);
        }
    }
}
